<template>
  <el-dialog :title=title
             v-if="open"
             :visible.sync="open"
             :modal= true
             width="80%"
             center
  >
  <div class="app-container">
    <div style="font-size: 20px;">订单信息</div>
    <!--横线组件纯纯为了页面好看-->
    <el-divider></el-divider>
    <el-form ref="form" :model="form"  label-width="150px" style="margin-left: 160px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="采购单号" prop="orderCode">
            <el-input v-model="form.orderCode" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="form.status" style="width: 100%;" disabled>
              <el-option label="已审批" :value=0 />
              <el-option label="采购入库" :value=1 />
              <el-option label="确认入库" :value=2 />
              <el-option label="待确定" :value=3 />
              <el-option label="待审批" :value=4 />
              <el-option label="已驳回" :value=5 />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="计划编号" prop="planNumber">-->
<!--            <el-input v-model="form.planNumber" readonly/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="计划名称" prop="planName">-->
<!--            <el-input v-model="form.planName"  readonly />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row>
        <el-col :span="10">
          <el-form-item label="供应商" prop="vendorName">
            <el-input v-model="form.vendorName" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="供货日期" prop="materialDeadline">
            <el-date-picker clearable
                            v-model="form.materialDeadline"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择计划截止日期" style="width: 100%" readonly>
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="开始日期" prop="planDate">-->
<!--            <el-date-picker readonly-->
<!--                            v-model="form.planDate"-->
<!--                            type="date"-->
<!--                            value-format="yyyy-MM-dd"-->
<!--                            placeholder="请选择计划开始日期" style="width: 100%">-->
<!--            </el-date-picker>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="付款方式" prop="paymentType">-->
<!--            <el-select v-model="form.paymentType" placeholder="请选择付款方式" style="width: 100%;" disabled>-->
<!--              <el-option-->
<!--                v-for="dict in dict.type.erp_payment_type"-->
<!--                :key="dict.value"-->
<!--                :label="dict.label"-->
<!--                :value="parseInt(dict.value)"-->
<!--              ></el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row>
        <el-col :span="10">
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="form.contact" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="联系人电话" prop="contactTel">
            <el-input v-model="form.contactTel" readonly/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="采购人员" prop="buyer">
            <el-input v-model="form.buyer" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="采购部门" prop="buyerDept">
            <el-input v-model="form.buyerDept" readonly/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="21">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注"  style="width: 95%" maxlength="250" readonly/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <!--横线组件纯纯为了页面好看-->
    <el-divider></el-divider>
    <el-row style="margin-bottom: 20px;">
      <el-col :span="1.5">
        <div style="font-size: 20px; margin-top: 5px">采购物料</div>
      </el-col>
    </el-row>
    <el-table border :data="addGoodsList" show-summary :summary-method="getSummaries">
      <el-table-column label="计划编号" align="center" prop="planNumber"/>
      <el-table-column label="物料编号" align="center" prop="itemCode"/>
      <el-table-column label="物料名称" align="center" prop="itemName"/>
      <el-table-column label="规格型号" align="center" prop="specification"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
      <el-table-column label="分类" align="center" prop="itemTypeName"/>
      <el-table-column
        v-for="col in columnsConfig"
        :key="col.prop"
        :label="col.label"
        :prop="col.prop"
        :width="col.width"
        :align="col.align"
        v-if="col.show">
      </el-table-column>
      <el-table-column label="采购单价" align="center" prop="estimatedUnitPrice" />
      <el-table-column label="采购数量" align="center" prop="quantity" />
      <el-table-column label="采购总价" align="center" prop="total" />
      <el-table-column label="客户" align="center" prop="clientName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="图纸" align="center" prop="drawingNum" />
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="uploadDrawing(scope.row)"
          >上传图纸
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer" style="text-align:center;margin-top: 20px;">
      <el-button type="primary" @click="submitForm" >确认</el-button>
      <el-button type="info" @click="cancel">返 回</el-button>
    </div>
    <el-dialog title="上传图纸" :visible.sync="drawingDialog" width="80%" @close="init(form.id)" append-to-body>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="图纸名称" prop="drawingName">
          <el-input
            v-model="queryParams.drawingName"
            placeholder="请输入图纸名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="图号" prop="drawingCode">
          <el-input
            v-model="queryParams.drawingCode"
            placeholder="请输入图号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="图纸描述" prop="remark">
          <el-input
            v-model="queryParams.remark"
            placeholder="请输入图纸描述"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <FileUpload
            :limit="100"
            :fileSize="500"
            :fileType="fileType"
            :value="pictureList"
            :isShowTip="isShowTip"
            :uploadData="uploadData"
            @onUploaded="handleUploaded"
          >
          </FileUpload>
        </el-col>
        <el-col :span="5" style="margin-top: 5px;">
          <span style="color: red;">请上传格式为PDF的文件</span>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="drawingList"
        max-height="400"
        @cell-mouse-enter="handleCellEnter()"
        @cell-mouse-leave="handleCellLeave()"
      >
        <el-table-column label="图纸名称" align="center" prop="drawingName" />
        <el-table-column label="图号" align="center" prop="drawingCode" />
        <el-table-column label="图纸描述" align="center" prop="remark">
          <div class="item" slot-scope="scope">
            <el-input
              class="item_input"
              v-model="scope.row.remark"
              size="mini"
              type="textarea"
              style="width: 100%;"
              :title="scope.row.remark"
              @blur="handleBlur(scope.row)"
            />
          </div>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['mes:cg:order:uploadDrawing']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>


    </el-dialog>
    <!-- 添加或修改图纸集Line对话框 -->
    <el-dialog title="查看图纸" :visible.sync="drawingViewDialog" width="80%" append-to-body>
      <iframe
        :src="drawingInfo.drawingUrl"
        frameborder="0"
        scrolling="auto"
        style="width: 100%; height: 600px;"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="drawingViewDialog=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>

  </el-dialog>

</template>

<script>
import {getOrder} from "@/api/mes/cg/order";
import {
  listDrawing,
  getDrawing,
  updateDrawing,
  delDrawing
} from "@/api/mes/cg/drawing";
import FileUpload from "./fileUpload.vue";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";

export default {
  name: "Drawing",
  dicts: ['erp_payment_type'],
  components: { FileUpload },
  data() {
    return {
      open:false,
      drawingDialog:false,
      drawingViewDialog:false,
      optType: undefined,
      title:null,
      // 遮罩层
      loading: true,
      // 添加的商品信息表格数据
      addGoodsList: [],
      drawingList:[],
      // 表单参数
      form: {
      },
      // 表单参数
      drawingInfo: {
        drawingUrl:null
      },
      // 显示图纸搜索条件
      showSearch: true,
      queryParams:{
        orderLineId:  null,
        drawingCode:null,
        drawingName:null,
      },
      // 上传类型
      fileType: ["pdf"],
      // 图纸列表
      pictureList: [],
      // 上传提示开关
      isShowTip: false,
      // 上传附带参数
      uploadData: { },
      // 可修改单元格属性
      editProp: ['remark'],
      columnsConfig:[],
    };
  },
  created() {
    this.getItemBase();
   /* this.getList();
    //查询商品表方法
    this.getGoodsList();
    //查询供应商列表方法
    this.getSupplierlist();*/
  },
  methods: {
    init(id){
      getOrder(id).then(response => {
        this.form = response.data;
        this.addGoodsList=response.data.cgPurchaseOrderDetailsList;
      });
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    //商品小计
    getSummaries(param) {
      const {columns, data} = param;
      const sums = []; //声明变量
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "小计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (
          column.property === "total"
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]+=' 元'
        } else {
          sums[index] = '';
        }
      });
      return sums;
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    submitForm() {
      let that=this;
      that.$parent.getList();
      this.open = false;
    },
    uploadDrawing(row) {
      this.drawingDialog  = true;
      this.queryParams.orderLineId = row.id;
      this.uploadData  = {orderLineId:row.id}
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + row.drawingCode + '"的图纸？').then(function() {
        return delDrawing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 上传成功
    handleUploaded(data) {
      this.$modal.msgSuccess("上传成功")
      this.getList();
    },
    // 查看图纸
    handleView(row) {
      this.drawingInfo.drawingUrl = row.drawingUrl;
      this.drawingViewDialog = true;

    },
    // 列表单元格鼠标进入事件
    handleCellEnter (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'block'
        }
      }
    },
    // 列表单元格鼠标离开事件
    handleCellLeave (row, column, cell, event) {
      if (column) {
        const property = column.property
        if (this.editProp.includes(property)) {
          cell.querySelector('.item_input').style.display = 'none'
        }
      }
    },
    // 列表单元格输入框失去焦点事件
    handleBlur(row) {
      updateDrawing(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
    getList(){
      listDrawing(this.queryParams).then(response => {
        this.drawingList = response.data;
        this.loading = false;
      });

    }
  }
};
</script>
